<?php
session_start();

if (!$_SESSION['loggedin']) {
    // User not logged in, redirect to login page
    echo "<script language='javascript'>window.location='index.php'</script>";
}
if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'registrar') {
?>
    <html>
        <head>
            <link rel="stylesheet" href="css/style.css" type="text/css" id="" media="print, projection, screen" />
            <script type="text/javascript" src="script/jquery-latest.js"></script>
            <script type="text/javascript" src="script/jquery.validate.js"></script>
            <script type="text/javascript">
                $(document).ready(function(){
                    $("#commentForm").validate({
                        rules: {
                            password: "required",
                            password_again: {
                                equalTo: "#password"
                            }
                        }
                    });
                });

                function confirmLogout()
                {
                    var agree=confirm("Are you sure you want to Logout?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }
                function confirmSubmit()
                {
                    var agree=confirm("Are you sure all fields entered are correct?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }
                function confirmReset()
                {
                    var agree=confirm("Are you sure you want to reset all fields?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }

            </script>

            <title>TUP Student Management</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>

        <body style=" height:100%; width: 100%; background-image: url(image/wall6.jpg);">

            <div id="container" style="height:auto; width: 100%;">

                <div style="background-image: url(image/wall12.jpg); height: 110px; width: auto; ">
                </div>
                <div align="center" style="background-image: url(image/wall11.jpg);  height: 74px; width: auto;">
                    <div align="center" style=" height: 4px; width: auto;"></div>
                    <div align="center" style=" height: 22px; width: auto;">
                        <a href="TMMViewProf.php"><b>Professor Management</b></a>&nbsp;&nbsp;
                        <a href="SMMViewStud.php"><b>[ Student Management ]</b></a>&nbsp;&nbsp;
                        <a href="CMMViewCourse.php"><b>Course Management</b></a>&nbsp;&nbsp;
                        <a href="EMSelectProf.php"><b>Enrollment</b></a>
                    </div>
                    <div align="center" style=" height: 4px; width: auto;"></div>
                    <div align="center" style=" height: 22px; width: auto;">
                        <a href="SMMAddStud.php" >[ Add Student ]</a>&nbsp;
                        <a href="SMMViewStud.php">View Student</a>&nbsp;
                        <a href="SMMSearchStud.php">Search Student</a>&nbsp;
                    </div>

                    <div  align="left" style="padding-left: 4%; height: 18px; width: 46%;float:left;"><?php echo "<aa class=\"smallw\"><b>Welcome " . $_SESSION['fname'] . ", Last login: " . $_SESSION['lastlogin'] . "</b></aa>"; ?>
                    </div>
                    <div align="right" style="padding-right: 4%; height: 18px; width: 46%;float:left;"><bb>|</bb>
                        <a class="small" href="RegEditProfile.php"><b>Edit Profile</b></a>
                        <bb>|</bb>
                        <a onclick="return confirmLogout()" class="small" href="Logout.php"><b>Logout</b></a>
                        <bb>|</bb></div>
                </div>

                <div align="left" style="padding-left: 4%; height: auto;  float:left; width: 96%;">

                    <h2>Add Student</h2>

                <?php
                if (isset($_POST['Submit'])) {
                    $fname = ucwords($_POST['fname']);
                    $mname = strtoupper($_POST['mname']);
                    $lname = ucwords($_POST['lname']);
                    $address = ucwords($_POST['address']);
                    $email = $_POST['email'];
                    $celno = $_POST['celno'];
                    $telnop1 = $_POST['telnop1'];
                    $telnop2 = $_POST['telnop2'];
                    $telnop3 = $_POST['telnop3'];
                    $gender = $_POST['gender'];
                    $mm = $_POST['mm'];
                    $dd = $_POST['dd'];
                    $yyyy = $_POST['yyyy'];
                    //$studidno
                    $dash = '-';
                    $birthdate = $yyyy . $dash . $mm . $dash . $dd;
                    $telno = $telnop1 . $telnop2 . $telnop3;
                    // telnoarea
                    include 'DBLibrary/config.php';
                    include 'DBLibrary/openDB.php';

                    mysql_select_db($dbname);
                    $rowedit = mysql_fetch_array(mysql_query("SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 8 HOUR), '%y') as yyyy"));
                    $studidno = $rowedit['yyyy'] . str_pad($mm, 2, '0', STR_PAD_LEFT) . str_pad($dd, 2, '0', STR_PAD_LEFT) . str_pad(mt_rand(0, 99), 2, '0', STR_PAD_LEFT);


                    $query = "INSERT INTO student(studidno, fname, mname, lname, gender, address, birthdate, telno, celno, email, validate)
                            VALUES('$studidno', '$fname', '$mname', '$lname', '$gender','$address', '$birthdate','$telno','$celno', '$email', 'activated' ) ";

                    $queryvalue = mysql_query($query);
                    if ($queryvalue == 0) {
                        echo "<a class=\"red\">" . mysql_errno($conn) . ": " . mysql_error($conn) . "</a>\n";
                        echo "<h3 class=\"err\">Input failed</h3>";
                    } else if ($queryvalue == 1)
                        echo "<h3 class=\"err\">Student has been added</h3>";

                    include 'DBLibrary/closeDB.php';
                } ?>


                <h3>Student Information</h3>
                <form onsubmit="return confirmSubmit()" class="cmxform" id="commentForm" name="commentForm" method="post" action="SMMAddStud.php">
                    <p>
                        <br>*First Name MI. Last Name:&nbsp;<input id="fname" type="text" name="fname" value="" size="14" maxlength="32" class="required" minlength="1"/>&nbsp;&nbsp;<input id="mname" type="text" name="mname" value="" size="1" maxlength="1" class="required" minlength="1"/>.&nbsp;&nbsp;<input id="lname" type="text" name="lname" value="" size="10" maxlength="32" class="required" minlength="1"/><br>
                        <br>*Gender: Male&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="gender" value="Male" class="required"/>
                        <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Female<input type="radio" name="gender" value="Female"/><br>
                        <br>*Telephone Number:&nbsp;<input id="telnop1" type="text" name="telnop1" value="" size="3" maxlength="3" class="required digits" minlength="3"/>
                        -<input id="telnop2" type="text" name="telnop2" value="" size="2" maxlength="2" class="required digits" minlength="2"/>
                        -<input id="telnop3" type="text" name="telnop3" value="" size="2" maxlength="2" class="required digits" minlength="2"/>
                        <br>
                        <br>
                        <label for="btm">*Birth Date:</label>
                        <select id="btm" class="required" name="mm" style="margin-left: 4px;" tabindex="8">
                            <option value="">-Month-</option>
                            <option value="1" >January</option>
                            <option value="2" >February</option>
                            <option value="3" >March</option>
                            <option value="4" >April</option>
                            <option value="5" >May</option>
                            <option value="6" >June</option>
                            <option value="7" >July</option>
                            <option value="8" >August</option>
                            <option value="9" >September</option>
                            <option value="10" >October</option>
                            <option value="11" >November</option>
                            <option value="12" >December</option>
                        </select>

                        <select id="btd" class="required" name="dd" style="margin-left: 4px;" tabindex="8">
                            <option value="">-Day-</option>
                            <option value="1" >1</option>
                            <option value="2" >2</option>
                            <option value="3" >3</option>
                            <option value="4" >4</option>
                            <option value="5" >5</option>
                            <option value="6" >6</option>
                            <option value="7" >7</option>
                            <option value="8" >8</option>
                            <option value="9" >9</option>
                            <option value="10" >10</option>
                            <option value="11" >11</option>
                            <option value="12" >12</option>
                            <option value="13" >13</option>
                            <option value="14" >14</option>
                            <option value="15" >15</option>
                            <option value="16" >16</option>
                            <option value="17" >17</option>
                            <option value="18" >18</option>
                            <option value="19" >19</option>
                            <option value="20" >20</option>
                            <option value="21" >21</option>
                            <option value="22" >22</option>
                            <option value="23" >23</option>
                            <option value="24" >24</option>
                            <option value="25" >25</option>
                            <option value="26" >26</option>
                            <option value="27" >27</option>
                            <option value="28" >28</option>
                            <option value="29" >29</option>
                            <option value="30" >30</option>
                            <option value="31" >31</option>
                        </select>

                        <select id="bty" class="required" name="yyyy" style="margin-left: 4px;" tabindex="8">
                            <option value="">-Year-</option>
                            <option value="2011" >2011</option>
                            <option value="2010" >2010</option>
                            <option value="2010" >2010</option>
                            <option value="2009" >2009</option>
                            <option value="2008" >2008</option>
                            <option value="2007" >2007</option>
                            <option value="2006" >2006</option>
                            <option value="2005" >2005</option>
                            <option value="2004" >2004</option>
                            <option value="2003" >2003</option>
                            <option value="2002" >2002</option>
                            <option value="2001" >2001</option>
                            <option value="2000" >2000</option>
                            <option value="1999" >1999</option>
                            <option value="1998" >1998</option>
                            <option value="1997" >1997</option>
                            <option value="1996" >1996</option>
                            <option value="1995" >1995</option>
                            <option value="1994" >1994</option>
                            <option value="1993" >1993</option>
                            <option value="1992" >1992</option>
                            <option value="1991" >1991</option>
                            <option value="1990" >1990</option>
                        </select><br>

                        <br>*Cell phone Number:&nbsp;0&nbsp;<input id="celno" type="text" name="celno" value="" size="10" maxlength="10" class="required digits" minlength="10"/><br>
                        <br>*Address:&nbsp;<input id="address" type="text" name="address" value="" size="50" maxlength="50" class="required" minlength="1"/><br>
                        <br>*Email Address:&nbsp;<input id="email" type="text" name="email" value="" size="32" maxlength="64" class="required email" minlength="1"/><br>
                        <br><br><input name="Submit" class="submit" type="submit" value="Submit"/><input type="reset" onclick ="return confirmReset()" value="Reset"/>
                    </p></form>
            </div>
        </div>

        <div id="footer" style="background-image: url(image/wall11.jpg);clear:both;text-align:center; height: 26px; width:auto;">
            <div align="center" style=" height: 4px; width: auto;"></div>
            <bb class ="small">Copyright &#169; 2011</bb>
        </div>

    </body>
</html>
<?php
            } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'admin') {
                echo "<script language='javascript'>window.location='UMMViewUser.php'</script>";
            } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'professor') {
                echo "<script language='javascript'>window.location='AMMSelectSection.php'</script>";
            }
?>